/* Copyright 2012 Nathan Begle
    This file is part of pcoursemanager.

    pcoursemanager is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    pcoursemanager is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with pcoursemanager.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;

import javax.swing.GroupLayout;
import javax.swing.JComboBox;

public class Add_Window {
	Frame frame;
	Label title_lbl;
	Label year_lbl;
	Label semester_lbl;
	Label crn_lbl;
	
	JComboBox comboBox;
	TextField year_fld;
	TextField crn_fld;
	
	GroupLayout cont_layout;
	GroupLayout main_layout;
	
	Panel main_panel;
	Panel cont_panel;
	
	Button add_bt;
	
	public Add_Window(ActionListener buttonAction, String buttonCommand, WindowListener window_listener)
	{
		String[] semesterArray = {"Fall", "Spring", "Summer"};
		Image image = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("Content/pcm_main.png"));
		frame = new Frame("Add Course");
		frame.setIconImage(image);
		addWindowListener(window_listener);
		
		main_panel = new Panel();
		cont_panel = new Panel();
		
		main_layout = new GroupLayout(main_panel);
		main_panel.setLayout(main_layout);
		main_layout.setAutoCreateGaps(true);
		
		cont_layout = new GroupLayout(cont_panel);
		cont_panel.setLayout(cont_layout);
		cont_layout.setAutoCreateGaps(true);
		
		
		title_lbl = new Label("Enter a new course...");
		year_lbl = new Label("Year:");
		semester_lbl = new Label("Semester:");
		crn_lbl = new Label("CRN:");
		
		comboBox = new JComboBox(semesterArray);
		comboBox.setBackground(Color.white);
		comboBox.setFocusable(true);
		
		year_fld = new TextField();
		year_fld.setPreferredSize(new Dimension(150, 20));
		year_fld.setFocusable(true);
		
		crn_fld = new TextField();
		crn_fld.setPreferredSize(new Dimension(150, 20));
		crn_fld.setFocusable(true);
		
		add_bt = new Button("Add Course");
		addButtonAction(buttonAction, buttonCommand);
		add_bt.setPreferredSize(new Dimension(150, 30));
		add_bt.setFocusable(true);
		
		setComponentLayout();
		
		setMainLayout();

		frame.add(main_panel);
		frame.pack();
		frame.setResizable(true);
		frame.setLocation(new Point(Toolkit.getDefaultToolkit().getScreenSize().width / 2 - frame.getSize().width / 2,
				Toolkit.getDefaultToolkit().getScreenSize().height / 2 - frame.getSize().height / 2));
		
		frame.setVisible(true);
	}
	
	void setMainLayout()
	{
		main_layout.setVerticalGroup(main_layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(main_layout.createParallelGroup()
						.addComponent(title_lbl))
				.addGroup(main_layout.createParallelGroup()
						.addComponent(cont_panel))
				.addGroup(main_layout.createParallelGroup()
						.addComponent(add_bt))
						.addContainerGap());
		main_layout.setHorizontalGroup(main_layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(main_layout.createParallelGroup()
						.addComponent(title_lbl).addComponent(cont_panel).addComponent(add_bt)).addContainerGap());
	}
	
	void setComponentLayout()
	{
		cont_layout.setVerticalGroup(cont_layout.createSequentialGroup()
				.addGroup(cont_layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(year_lbl).addComponent(year_fld))
				.addGroup(cont_layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(semester_lbl).addComponent(comboBox))
				.addGroup(cont_layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(crn_lbl).addComponent(crn_fld)));
		cont_layout.setHorizontalGroup(cont_layout.createSequentialGroup()
				.addGroup(cont_layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(year_lbl).addComponent(semester_lbl).addComponent(crn_lbl))
				.addGroup(cont_layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(year_fld).addComponent(comboBox).addComponent(crn_fld)));
	}
	
	public void bring_to_front()
	{
		frame.toFront();
	}
	
	public int getYear()
	{
		try
		{
			return Integer.parseInt(year_fld.getText());
		} catch(NumberFormatException e)
		{
			errYear();
			return -1;
		}
	}
	
	public int getSemester()
	{
		return comboBox.getSelectedIndex() + 1;
	}
	
	public long getCRN()
	{
		try
		{
			return Long.parseLong(crn_fld.getText());
		} catch (NumberFormatException e) {
			errCRN();
			return -1;
		}
	}
	
	public void resetWindow()
	{
		comboBox.setSelectedIndex(0);
		crn_fld.setText("");
		year_fld.setText("");
		crn_fld.setBackground(Color.white);
		year_fld.setBackground(Color.white);
		comboBox.setBackground(Color.white);
	}
	
	public void errCRN()
	{
		crn_fld.setBackground(new Color(1.0f, 0.7f, 0.7f));
	}
	
	public void errSemester()
	{
		comboBox.setBackground(new Color(1.0f, 0.7f, 0.7f));
	}
	
	public void errYear()
	{
		year_fld.setBackground(new Color(1.0f, 0.7f, 0.7f));
	}
	
	public void addButtonAction(ActionListener actList, String command)
	{
		add_bt.setActionCommand(command);
		add_bt.addActionListener(actList);
	}
	
	public void addWindowListener(WindowListener listener)
	{
		frame.addWindowListener(listener);
	}
	
	public void disposeWindow()
	{
		frame.dispose();
	}
	
	public boolean hasFocus()
	{
		return frame.isFocused();
	}
}
